<?php
class SP_Common_User
{
	protected static $session_name = '_SP_USER_';
	protected static $check_time_need = false;
	
	public static function logout()
	{
		SP_Common_Session::set(self::$session_name,array());
	}
	
	public static function setAccess($name)
	{
		$user = SP_Common_Session::get(self::$session_name,array());
		$user['acces'][$name]=true;
		SP_Common_Session::set(self::$session_name,$user);
		self::setLastActive(time());
	}
	
	public static function getAccess($name)
	{
		if(self::$check_time_need)
		{
			if(!self::checkActiveTime()) return false;
		}
		$user = SP_Common_Session::get(self::$session_name,array());
		return $user['acces'][$name];
	}
	
	public static function delAccess($name)
	{
		$user = SP_Common_Session::get(self::$session_name,array());
		unset($user['acces'][$name]);
		SP_Common_Session::set(self::$session_name,$user);
	}
	
	public static function checkAccesses($names, $for_all=true)
	{
		$names=(array)$names;
		if($for_all)
		{
			foreach($names as $name)
			{
				if(!self::getAccess($name)) return false;
			}
			return true;
		}
		else
		{
			foreach($names as $name)
			{
				if(self::getAccess($name)) return true;
			}
			return false;
		}
	}
	
	public static function setLogin($name)
	{
		$user = SP_Common_Session::get(self::$session_name,array());
		$user['login']=$name;
		SP_Common_Session::set(self::$session_name,$user);
	}
	
	public static function getLogin($name)
	{
		$user = SP_Common_Session::get(self::$session_name,array());
		return $user['login'];
	}
	
	public static function setActiveTime($seconds)
	{
		$user = SP_Common_Session::get(self::$session_name,array());
		$user['active_time']=$seconds;
		SP_Common_Session::set(self::$session_name,$user);
	}
	
	public static function getActiveTime($seconds)
	{
		$user = SP_Common_Session::get(self::$session_name,array());
		return $user['active_time'];
	}
	
	public function setLastActive($seconds)
	{
		$user = SP_Common_Session::get(self::$session_name,array());
		$user['last_active']=$seconds;
		SP_Common_Session::set(self::$session_name,$user);
	}
	
	public function checkActiveTime()
	{
		$user = SP_Common_Session::get(self::$session_name,array());
		
		if (time()-$user['last_active'] > $user['active_time'])
		{
			self::logout();
			return false;
		}
		else
		{
			self::setLastActive(time());
			return true;
		}
	}
	
	public function authorisation()
	{
		if ($bool)
		{
			self::setAccess('user');
		}
		else
		{
			self::logout();
		}
	}
	
	public function isAuthorised()
	{
		$user = SP_Common_Session::get(self::$session_name,array());
		return (bool)count($user['acces']);
	}
	
	public function setProperty($name,$value)
	{
		$user = SP_Common_Session::get(self::$session_name,array());
		$user['properties'][$name]=$value;
		SP_Common_Session::set(self::$session_name,$user);
	}
	
	public function hasProperty($name)
	{
		$user = SP_Common_Session::get(self::$session_name,array());
		return isset($user['properties'][$name]);
	}
	
	public function getProperty($name,$value=NULL)
	{
		$user = SP_Common_Session::get(self::$session_name,array());
		
		return self::hasProperty? $user['properties'][$name] : $value;
	}
}